package com.thinvent.recommend.manager.manager.impl;

import com.thinvent.recommend.common.enums.FileHistoryActionTypeEnums;
import com.thinvent.recommend.entity.KbFileHistory;
import com.thinvent.recommend.manager.dto.UserBehaviorFilesDTO;
import com.thinvent.recommend.manager.manager.UserBehaviorDataManager;
import com.thinvent.recommend.mapper.KbFileHistoryMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * TODO 直接拉数据源的数据，后续可以改为rpc服务或者rest服务获取数据
 */
@Component
public class UserBehaviorDataManagerImpl implements UserBehaviorDataManager {
    @Resource
    private KbFileHistoryMapper kbFileHistoryMapper;

    @Override
    public List<UserBehaviorFilesDTO> getUserBehaviorData(String userId) {
        // 结果集~
        List<UserBehaviorFilesDTO> userBehaviorsDTOList = new ArrayList<>();

        // 用户历史记录表中的预览、下载、编辑以及点赞和收藏的文件数据
        List<KbFileHistory> kbFileHistorys = kbFileHistoryMapper.selectByUserAndActionTypes(userId,
                Arrays.asList(FileHistoryActionTypeEnums.PREVIEW.getCode(),
                        FileHistoryActionTypeEnums.EDIT.getCode(),
                        FileHistoryActionTypeEnums.DOWNLOAD.getCode(),
                        FileHistoryActionTypeEnums.LIKE.getCode(),
                        FileHistoryActionTypeEnums.FAVORITES.getCode()));
        if(CollectionUtils.isNotEmpty(kbFileHistorys)) {
            List<UserBehaviorFilesDTO> userBehaviorFilesDTOS = convertHistoryToDto(kbFileHistorys);
            userBehaviorsDTOList.addAll(userBehaviorFilesDTOS);
        }
        return userBehaviorsDTOList;
    }

    /**
     * 用户动作 操作类型 0 上传  1编辑 2 预览 3下载 4 移动 5 拷贝 6删除 7分享 8转发
     */
    private List<UserBehaviorFilesDTO> convertHistoryToDto(List<KbFileHistory> histories) {
        List<UserBehaviorFilesDTO> dtoList = new ArrayList<>();
        if (histories == null) {
            return dtoList;
        }
        for (KbFileHistory h : histories) {
            UserBehaviorFilesDTO dto = new UserBehaviorFilesDTO();
            dto.setFildId(h.getFileId());
            dto.setUserId(h.getUserId());
            if(h.getActionType().equals(FileHistoryActionTypeEnums.EDIT.getCode())) {
                dto.setType(FileHistoryActionTypeEnums.EDIT.getDescription());
            }
            if(h.getActionType().equals(FileHistoryActionTypeEnums.PREVIEW.getCode())) {
                dto.setType(FileHistoryActionTypeEnums.PREVIEW.getDescription());
            }
            if(h.getActionType().equals(FileHistoryActionTypeEnums.DOWNLOAD.getCode())) {
                dto.setType(FileHistoryActionTypeEnums.DOWNLOAD.getDescription());
            }
            if(h.getActionType().equals(FileHistoryActionTypeEnums.LIKE.getCode())) {
                dto.setType(FileHistoryActionTypeEnums.LIKE.getDescription());
            }
            if(h.getActionType().equals(FileHistoryActionTypeEnums.FAVORITES.getCode())) {
                dto.setType(FileHistoryActionTypeEnums.FAVORITES.getDescription());
            }
            dtoList.add(dto);
        }
        return dtoList;
    }
}
